Let's start with an example, in which we want to compute a = a + 1 five times with the initial value a = 0:
a = 0;
a = a + 1;
a = a + 1;
a = a + 1;
a = a + 1;
a = a + 1;
Alternatively, this can be achieved by using a for-loop with fewer lines of code:
a = 0;
for i = 1:5
a = a + 1;
end
The vector 1:5 here controls how many times the statement a = a + 1 will be repeated.
The example below computes
A naive way is to repeat the addition five times:
S = 0;
s = s + 1/2^1;
s = s + 1/2^2;
s = s + 1/2^3;
s = s + 1/2^4;
s = s + 1/2^5;
Actually, it evaluates the statement s = s + 1/2^i repeatedly with i equal to 1, 2, ..., 5. This observation leads us to using the loop below:
s = 0;
for i = 1:5
s = s + 1/2^i;
end
For convenience of our discussion, let's call statements sandwiched by for and end the body. The vector 1:5 has 5 elements, hence the loop body will be executed 5 times. The value of i is taken from an element of the vector, in such a way that
i takes the first element (i.e., 1) when the body is evaluated for the first time,i takes the second element (i.e., 2) when the body is evaluated for the second time,To demonstrate that
converges to 1, you may use i = 1:40 as shown below. It returns a sum extremely close to 1.
clear
s = 0;
for i = 1:40
s = s + 1/2^i;
end
s
All variables cleared
s = 1e-1 ×
9.999999999990905
A for-loop takes the following general form:
for variable = vector
statement_1
statement_2
...
statement_n
end
Here, vector is a row or column vector. If vector is empty, none of the statements in the body will be executed. The vector vector is usually constructed in the for line by the colon : operator. Nevertheless, it can also be constructed elsewhere, as shown in the example below.
In the example below, the vector v is constructed before entering the loop.
s = 0;
% v is a vector of integers from 1 to 40.
v = 1:40;
for i = v
s = s + 1/2^i;
end
s
s = 1e-1 ×
9.999999999990905
Instead of looping through the integers
The following implementation gives exactly the same result.
s = 0;
% v is a vector of the numbers 1/2^1, 1/2^2, ..., 1/2^40.
v = 1./2.^(1:40);
for i = v
s = s + i;
end
s
s = 1e-1 ×
9.999999999990905
The general syntax shown here is the most common form. Nevertheless, a for-loop can also be written in one single line using separators , or ;. All of the examples below give the same a as the final results.
disp('Example 1. Using separators , and ,')
a = 0;
for i = 1:3, a = a + i, end
a
disp('Example 2. Using separators ; and ;')
a = 0;
for i = 1:3; a = a + i; end
a
disp('Example 3. Using separators , and ;')
a = 0;
for i = 1:3, a = a + i; end
a
Example 1. Using separators , and ,
a =
1.0000
a =
3.0000
a =
6.0000
a =
6.0000
Example 2. Using separators ; and ;
a =
6.0000
Example 3. Using separators , and ;
a =
6.0000
In the above example, different components of the for-loop are separated by separators. A separator can be a comma, a semicolon, or a newline character.
⚠️⚠️⚠️ The end keyword should be followed by a newline character. This is different from MATLAB in which the end can be followed by other separators.
A loop can be embedded within another loop, as shown in the example below.
N = 6;
for i = 1:3
p(i) = 0.0;
for j = 1:N
p(i) = p(i) + j^i;
end
end
p
p =
21.000 91.000 441.00
v= rand(1,5) + rand(1,5)*i
for i = v
% Computing the length.
abs(i)
end
ans =
1.2290
ans = 1e-1 ×
8.4194
ans = 1e-1 ×
6.7398
ans = 1e-1 ×
7.2952
ans = 1e-1 ×
7.1020
v='loop through characters'
for c = v
disp(c)
end
l
o
o
p
t
h
r
o
u
g
h
c
h
a
r
a
c
t
e
r
s